<?php
include_once $REP_CLASS . 'Selector.php';

class Annonce {

private $refannonce  = 0;
private $typeannonce;
private $titre;
private $description;
private $nature;
private $dateannonce;
private $periodedu;
private $periodeau;
private $refutilisateur;
private $email;

private $refassociation;
private $insee;
private $dept;

private $assocPDO;
private $sqlInsert;
private $reqInsert;
private $sqlDelete;
private $reqDelete;
private $sqlUpdate;
private $reqUpdate;

/**
 * Constructeur
 * @db : la connexion de la base de donn?e
 */
 function __construct($db){

      // Cr?ation de la connexion
      $this->assocPDO = $db;
      // Initialisation des requ?tes
      $this->initRequete();
      // Pr?paration des requ?tes
      $this->prepare();

}
/**
 * Initialisation des requ?tes pr?par?es
 */
private function initRequete(){

      $this->sqlInsert = "INSERT INTO annonce (typeannonce, titre, description, nature, dateannonce, periodedu, periodeau, refutilisateur, email, refassociation, insee, dept) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
      $this->sqlDelete = "DELETE FROM annonce WHERE `refannonce` = ?";
      $this->sqlUpdate = "UPDATE annonce SET typeannonce=?, titre=?, description=?, nature=?, dateannonce=?, periodedu=?, periodeau=?, refutilisateur=?, email=? WHERE `refannonce` = ?";

}
/**
 * Pr?paration des requ?tes
 */
private function prepare(){

      $this->reqInsert       = $this->assocPDO->prepare($this->sqlInsert);
      $this->reqDelete       = $this->assocPDO->prepare($this->sqlDelete);
      $this->reqUpdate       = $this->assocPDO->prepare($this->sqlUpdate);

}
/**
 * Insertion en base des infos de l'utilisateur
 */
public function insertion(){

      // Requete d'insertion
      $champs = array($this->typeannonce, $this->titre, $this->description, $this->nature, $this->dateannonce, $this->periodedu, $this->periodeau, $this->refutilisateur, $this->email, $this->refassociation, $this->insee, $this->dept);

      try
      {
         return $this->reqInsert->execute($champs);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqInsert->errorInfo());
         throw new Exception('Une erreur est survenue lors de l\'insertion dans annonce.' . $e->getMessage());
      }

}
/**
 * Modification des infos de l'utilisateur
 */
public function modification(){

      // Requete de modification
      $champs = array($this->typeannonce, $this->titre, $this->description, $this->nature, $this->dateannonce, $this->periodedu, $this->periodeau, $this->refutilisateur, $this->email, $this->refassociation, $this->insee, $this->dept);

      try
      {
         return $this->reqUpdate->execute($champs);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqUpdate->errorInfo());
         throw new Exception('Une erreur est survenue lors de la modification dans annonce.');
      }

}
/**
 * Suppression de l'utilisateur ? partir de son ID
 */
public function suppression(){

      // Requete de suppression
      try
      {
        return $this->reqDelete->execute(array($this->refannonce));
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqDelete->errorInfo());
         throw new Exception('Une erreur est survenue lors de la suppression dans annonce.');
      }

}
/**
 * Select
 */
public function select(){

      $select = new Selector();
      $champs = array("refannonce"=>$this->refannonce, "typeannonce"=>$this->typeannonce, "titre"=>$this->titre, "description"=>$this->description, "nature"=>$this->nature, "dateannonce"=>$this->dateannonce, "periodedu"=>$this->periodedu, "periodeau"=>$this->periodeau, "refutilisateur"=>$this->refutilisateur, "email"=>$this->email, "refassociation"=>$this->refassociation, "insee"=>$this->insee, "dept"=>$this->dept);

      foreach($champs as $cle=>$valeur)
      {
        // Les Champs
        $select->addChamp($cle);
        // Les Crit?res
        if( !empty($valeur) )
         $select->addCritere($cle, $valeur);
      }

      // La Table
      $select->addFrom('annonce');

      //echo 'requete : ' . $select->toString();
      return $this->assocPDO->query($select->toString());

}
/**
 * Charge les infos de annonce
 */
public function charge(){

    $user = new Annonce($this->assocPDO);
    $user->setRefannonce($this->getRefannonce());
    // Lecture de la premi?re ligne
    $stmt = $user->select();
    $result = $stmt->fetch();

    $this->refannonce=$result['refannonce'];
	$this->typeannonce=$result['typeannonce'];
	$this->titre=$result['titre'];
	$this->description=$result['description'];
	$this->nature=$result['nature'];
	$this->dateannonce=$result['dateannonce'];
	$this->periodedu=$result['periodedu'];
	$this->periodeau=$result['periodeau'];
	$this->refutilisateur=$result['refutilisateur'];
	$this->email=$result['email'];
	$this->refassociation=$result['refassociation'];
	$this->insee=$result['insee'];
	$this->dept=$result['dept'];

      // Fermeture du curseur
      $stmt->closeCursor();

}
/**
 * Affiche les infos de l'utilisateur
 */
public function affichage(){

      $champs = array("refannonce"=>$this->refannonce, "typeannonce"=>$this->typeannonce, "titre"=>$this->titre, "description"=>$this->description, "nature"=>$this->nature, "dateannonce"=>$this->dateannonce, "periodedu"=>$this->periodedu, "periodeau"=>$this->periodeau, "refutilisateur"=>$this->refutilisateur, "email"=>$this->email, "refassociation"=>$this->refassociation, "insee"=>$this->insee, "dept"=>$this->dept);
      echo "==== Fiche Annonce ==== <br>";
      foreach($champs as $cle=>$valeur)
      {
        echo $cle . " : " . $valeur;
        echo "<br>";
      }
      echo "==========================<br>";

}






/**
 * Select des PA selon les criteres saisis
 * Utilise pour les petites annonces
 */
public function selectAnnonce(){

      try
      {
      	 $req= "SELECT annonce.refannonce as refannonce,typeannonce,titre,description,nature,DATE_FORMAT(dateannonce,'%d/%m/%Y') as dateannonce,periodedu,periodeau,email,
      	 		refassociation,refutilisateur,annonce.insee,annonce.dept as dept,commune.libelle libcommune,commune.cp cp, dept.libelle as libdept
               FROM annonce left join commune on (commune.insee=annonce.insee)
               left join dept on (dept.dept=annonce.dept) where 1=1";

         if ($this->getRefannonce()!='')
         	$req=$req . " and refannonce='".$this->getRefannonce()."'";

         if ($this->getTypeannonce()!='')
         	$req=$req . " and typeannonce='".$this->getTypeannonce()."'";

         if ($this->getNature()!='')
         	$req=$req . " and nature='".$this->getNature()."'";

         if ($this->getDateannonce()!='')
         	$req=$req . " and dateannonce> str_to_date('".$this->getDateannonce()."','%d/%m/%Y')";

         if ($this->getInsee()!='')
         	$req=$req . " and annonce.insee='".$this->getInsee()."'";

         if ($this->getDept()!='')
         	$req=$req . " and annonce.dept='".$this->getDept()."'";

         if ($this->getDescription()!='')
         {	$req=$req . " and (description like '%".str_replace("'","''",$this->getDescription())."%' or titre like '%".str_replace("'","''",$this->getDescription())."%')";

         }
		 $req=$req . " order by annonce.dateannonce desc ";

      	 return $this->assocPDO->query($req);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'.$req;
         throw new Exception('Une erreur est survenue lors de la recherche des annonces.');
      }
}





/**
 * Retrieves the value from the field refannonce
 * @return String with the value of the field
 */
public function getRefannonce(){
  return $this->refannonce;
}
/**
 * Retrieves the value from the field typeannonce
 * @return String with the value of the field
 */
public function getTypeannonce(){
  return $this->typeannonce;
}
/**
 * Retrieves the value from the field titre
 * @return String with the value of the field
 */
public function getTitre(){
  return $this->titre;
}
/**
 * Retrieves the value from the field description
 * @return String with the value of the field
 */
public function getDescription(){
  return $this->description;
}
/**
 * Retrieves the value from the field nature
 * @return String with the value of the field
 */
public function getNature(){
  return $this->nature;
}
/**
 * Retrieves the value from the field dateannonce
 * @return String with the value of the field
 */
public function getDateannonce(){
  return $this->dateannonce;
}
/**
 * Retrieves the value from the field periodedu
 * @return String with the value of the field
 */
public function getPeriodedu(){
  return $this->periodedu;
}
/**
 * Retrieves the value from the field periodeau
 * @return String with the value of the field
 */
public function getPeriodeau(){
  return $this->periodeau;
}
/**
 * Retrieves the value from the field refutilisateur
 * @return String with the value of the field
 */
public function getRefutilisateur(){
  return $this->refutilisateur;
}
/**
 * Retrieves the value from the field email
 * @return String with the value of the field
 */
public function getEmail(){
  return $this->email;
}
/**
 * Retrieves the value from the field refassociation
 * @return String with the value of the field
 */
public function getRefassociation(){
  return $this->refassociation;
}/**
 * Retrieves the value from the field insee
 * @return String with the value of the field
 */
public function getInsee(){
  return $this->insee;
}/**
 * Retrieves the value from the field dept
 * @return String with the value of the field
 */
public function getDept(){
  return $this->dept;
}

/**
 * Set the value from the field refannonce
 * @param refannonce String with the value for the field
 */
public function setRefannonce($refannonce){
  $this->refannonce = $refannonce;
}
/**
 * Set the value from the field typeannonce
 * @param typeannonce String with the value for the field
 */
public function setTypeannonce($typeannonce){
  $this->typeannonce = $typeannonce;
}
/**
 * Set the value from the field titre
 * @param titre String with the value for the field
 */
public function setTitre($titre){
  $this->titre = $titre;
}
/**
 * Set the value from the field description
 * @param description String with the value for the field
 */
public function setDescription($description){
  $this->description = $description;
}
/**
 * Set the value from the field nature
 * @param nature String with the value for the field
 */
public function setNature($nature){
  $this->nature = $nature;
}
/**
 * Set the value from the field dateannonce
 * @param dateannonce String with the value for the field
 */
public function setDateannonce($dateannonce){
  $this->dateannonce = $dateannonce;
}
/**
 * Set the value from the field periodedu
 * @param periodedu String with the value for the field
 */
public function setPeriodedu($periodedu){
  $this->periodedu = $periodedu;
}
/**
 * Set the value from the field periodeau
 * @param periodeau String with the value for the field
 */
public function setPeriodeau($periodeau){
  $this->periodeau = $periodeau;
}
/**
 * Set the value from the field refutilisateur
 * @param refutilisateur String with the value for the field
 */
public function setRefutilisateur($refutilisateur){
  $this->refutilisateur = $refutilisateur;
}
/**
 * Set the value from the field email
 * @param email String with the value for the field
 */
public function setEmail($email){
  $this->email = $email;
}
/**
 * Set the value from the field refassociation
 * @param refassociation String with the value for the field
 */
public function setRefassociation($refassociation){
  $this->refassociation = $refassociation;
}
/**
 * Set the value from the field insee
 * @param insee String with the value for the field
 */
public function setInsee($insee){
  $this->insee = $insee;
}
/**
 * Set the value from the field dept
 * @param dept String with the value for the field
 */
public function setDept($dept){
  $this->dept = $dept;
}

}
?>
